from core.data.student2course_data_access import save_students2course,loads_students2course_json

# 课程信息字典
# courses = {
#     "CS101": "计算机科学导论",
#     "ENG201": "高级英语写作",
#     "MATH301": "线性代数",
#     "PHYS401": "物理学原理",
#     "HIST501": "世界历史概论"
# }

courses = {
"CS101": {"time":"周二 8:00-10:00","location":"教学楼101","credit": 3},
"ENG201": {"time":"周二 10:30-12:00","location":"教学楼102","credit": 2},
"MATH301":{"time":"周二 9:00-11:00","location":"教学楼103","credit": 3},
"PHYS401":{"time":"周四 8:00-10:00","location":"教学楼104","credit": 2},
"HIST501":{"time":"周五 8:00-10:00","location":"教学楼105","credit": 1},
}

# 学生选课字典
student_courses = loads_students2course_json()
def is_time_conflicts(time1, time2):
    day1,start_time1, end_time1 = parse_time(time1)
    day2,start_time2, end_time2 = parse_time(time2)
    if day1 != day2:
        return False
    if end_time1 < start_time2 or end_time2 < start_time1:
        return False

    return True

def parse_time(time1):
    day, time_range = time1.split(" ")
    start_time, end_time = time_range.split("-")
    start_time = float(start_time.replace(":", "."))
    end_time = float(end_time.replace(":", "."))
    return day, start_time, end_time
def select_course(student_id):
    # 选课
    print("当前登陆人：", student_id)
    print("展示可以选修课程：")
    print("*" * 50)
    # for course_code, course_name in courses.items():
    #     print(f"课程ID：{course_code}，课程名称：{course_name}")
    for course_code, course_dict in courses.items():
        print(f"课程ID：{course_code:10}，课程学分：{course_dict['credit']} 上课时间：{course_dict['time']} 上课地址：{course_dict['location']}")
    print("*" * 50)
    course_code = input("请输入选修课程ID")
    if student_id not in student_courses:
        student_courses[student_id] = {}

    # 每一个学生id在student_courses大字典中都有一个一个初始化的列表
    if course_code in student_courses[student_id]:
        return 501, course_code
    else:
        print(course_code)
        new_course_time = courses[course_code]["time"]

        for selected_course_code,_ in student_courses[student_id].items():
            selected_course_time = courses[selected_course_code]["time"]
            if is_time_conflicts(new_course_time, selected_course_time):
                print("当前新选择课程：",course_code,"时间：", new_course_time)
                print("已经选择课程：", selected_course_code, "时间：", selected_course_time)
                return 502, course_code
        student_courses[student_id][course_code] = None
        save_students2course(student_courses)
        #
        return 503, course_code

    # print("student_courses:", student_courses)

def drop_course(student_id):
    if student_id in student_courses:
        print("该账号已经选修课程：")
        print("-" * 50)
        for course_code,_ in student_courses[student_id].items():
            print(
                f"课程ID：{course_code:10}，课程学分：{courses[course_code]['credit']} 上课时间：{courses[course_code]['time']} 上课地址：{courses[course_code]['location']}")
        print("-" * 50)

        course_code = input("请输入您要退的课程ID：")
        if course_code in student_courses[student_id]:
            student_courses[student_id].pop(course_code)
            print(f"成功退课：{courses[course_code]}")
            save_students2course(student_courses)
            # print("student_courses:", student_courses)
            return 603, course_code
        else:
            # print("您未选修该课程")
            return 602, course_code
    else:
        # print("您未选修过任何课程！")
        return 601,None

def view_course(student_id):
    if student_id in student_courses:
        print("-" * 50)
        for course_code,_ in student_courses[student_id].items():
            # print(f"课程ID：{course_code}，课程名称：{courses.get(course_code)}")
            print(
                f"课程ID：{course_code:10}，课程学分：{courses[course_code]['credit']} 上课时间：{courses[course_code]['time']} 上课地址：{courses[course_code]['location']}")
        print("-" * 50)
    else:
        print("您未选修过任何课程！")

def set_score(student_id, course_code, score):
    if student_id in student_courses:
        print("course_code:::", course_code)
        print("student_courses[student_id]:::", student_courses[student_id])
        if course_code in student_courses[student_id]:
            student_courses[student_id][course_code] = score
            save_students2course(student_courses)
            return 200
        else:
            return 602
    else:
        return 601

def show_score(student_id):
    if student_id in student_courses:
        for course_code, score in student_courses[student_id].items():
            print(f"课程code:{course_code},课程分数{score}")
    else:
        return 601